package com.test.tom;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.FixtureDef;

import org.andengine.engine.Engine;
import org.andengine.engine.camera.Camera;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.ui.activity.BaseGameActivity;

/**
 * Created by Tom on 23/10/13.
 */
public class SceneManager {

    private AllScenes currentScene;
    private BaseGameActivity activity;
    private Engine engine;
    private Camera camera;
    private BitmapTextureAtlas splashTA;
    private ITextureRegion splashTR;
    BitmapTextureAtlas playerTexture;
    ITextureRegion playerTextureRegion;
    PhysicsWorld physicsWorld;
    private Scene splashScene, gameScene, menuScene;

    public enum SceneType
    {
        SCENE_SPLASH,
        SCENE_MENU,
        SCENE_GAME,
        SCENE_LOADING,
    }
    public SceneManager() {

    }
    public  SceneManager(BaseGameActivity inActivity, Engine inEngine, Camera inCamera)
    {
        this();
       this.activity = inActivity;
        this.engine = inEngine;
        this.camera = inCamera;
    }
    public void loadSplashResources()
    {
        splashTA = new BitmapTextureAtlas(activity.getTextureManager(),256,256);
        splashTR = BitmapTextureAtlasTextureRegionFactory.createFromAsset(splashTA,this.activity,"SplashScreen.png",0,0);
        splashTA.load();
    }
    public  void loadGameResources()
    {
        playerTexture = new BitmapTextureAtlas(activity.getTextureManager(),64,64);
        playerTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(playerTexture,this.activity,"player.png",0,0);
        playerTexture.load();
    }

    public Scene createGameScene() {
        gameScene = new Scene() {

            @Override
            public boolean onSceneTouchEvent(TouchEvent pSceneTouchEvent) {

                if(pSceneTouchEvent.getX() > camera.getCenterX())
                    camera.setRotation(camera.getRotation()+2);
                else
                    camera.setRotation(camera.getRotation()-2);
                Vector2 gravity = new Vector2(Vector2.Zero);
                float rot = (float)Math.toRadians(camera.getRotation());
                gravity.x = -(float)(9.81 * Math.sin(rot));
                gravity.y = -(float)(9.81 * Math.cos(rot));



                physicsWorld.setGravity(gravity);
                return super.onSceneTouchEvent(pSceneTouchEvent);
            }
        };
        physicsWorld = new PhysicsWorld(new Vector2( 0,-9.81f), false);
        final Sprite sPlayer = new Sprite(camera.getCenterX(), camera.getCenterY(),
                playerTextureRegion, engine.getVertexBufferObjectManager()) {


            @Override
            public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
                switch(pSceneTouchEvent.getAction()) {
                    case TouchEvent.ACTION_DOWN:
                        this.setColor((float)Math.random(),(float)Math.random(),(float)Math.random());
                        break;
                }

                    return true;
            }


        };

        gameScene.registerTouchArea(sPlayer);
        gameScene.setTouchAreaBindingOnActionDownEnabled(true);
        final FixtureDef PLAYER_FIX = PhysicsFactory.createFixtureDef(1.0f,0.75f, 0.0f);
        Body body = PhysicsFactory.createCircleBody(physicsWorld, sPlayer, BodyDef.BodyType.DynamicBody, PLAYER_FIX);
        gameScene.attachChild(sPlayer);
        body.setLinearVelocity(10, 8);
        gameScene.registerUpdateHandler(physicsWorld);
        createWalls();
        physicsWorld.registerPhysicsConnector(new PhysicsConnector(sPlayer, body, true, false));
        return gameScene;
    }



    private void createWalls() {
        FixtureDef WALL_FIX = PhysicsFactory.createFixtureDef(0.0f, 0.0f, 0.0f);
        Rectangle floor = new Rectangle(camera.getCenterX(), camera.getHeight() - 15, camera.getWidth(),
                15, engine.getVertexBufferObjectManager());
        Rectangle roof = new Rectangle(camera.getCenterX(),15,camera.getWidth(),15,this.engine.getVertexBufferObjectManager());
        Rectangle left = new Rectangle(15,camera.getCenterY(),15,camera.getHeight(),this.engine.getVertexBufferObjectManager());
        Rectangle right = new Rectangle(camera.getWidth()-15,camera.getCenterY(),15,camera.getHeight(),this.engine.getVertexBufferObjectManager());
        PhysicsFactory.createBoxBody(physicsWorld, floor, BodyDef.BodyType.StaticBody, WALL_FIX);
        PhysicsFactory.createBoxBody(physicsWorld, roof, BodyDef.BodyType.StaticBody, WALL_FIX);
        PhysicsFactory.createBoxBody(physicsWorld, left, BodyDef.BodyType.StaticBody, WALL_FIX);
        PhysicsFactory.createBoxBody(physicsWorld, right, BodyDef.BodyType.StaticBody, WALL_FIX);
        this.gameScene.attachChild(floor);
        this.gameScene.attachChild(roof);
        this.gameScene.attachChild(left);
        this.gameScene.attachChild(right);

    }
    public Scene getMenuScene() {
        return menuScene;
    }
    public Scene createSplashScene()
    {
        splashScene = new Scene();
        splashScene.setBackground(new Background(0,0,0));

        Sprite icon = new Sprite(0,0,splashTR,engine.getVertexBufferObjectManager());
        icon.setPosition(camera.getCenterX(),camera.getCenterY());
        icon.setScale(2);
        splashScene.attachChild(icon);
        return  splashScene;
    }
    public enum AllScenes {
        SPLASH,
        MENU,
        GAME
    }
    public AllScenes getCurrentScene() {
        return currentScene;
    }

    public void setCurrentScene(AllScenes currentScene) {
        this.currentScene = currentScene;
        switch (currentScene) {
            case SPLASH:
                engine.setScene(splashScene);
                break;
            case MENU:
                engine.setScene(menuScene);
                break;
            case GAME:
                engine.setScene(gameScene);
                break;
        }
    }



}

